{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1a0cf63",
   "metadata": {},
   "source": [
    "# Evaluate speaker verification with PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abdc0067",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "import sys\n",
    "os.chdir('../..')\n",
    "sys.path.insert(1, os.path.join(sys.path[0], '../..'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fccb6bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from sklearn.decomposition import PCA\n",
    "import numpy as np\n",
    "\n",
    "from notebooks.notebooks_utils import (\n",
    "    load_models,\n",
    "    evaluate_models,\n",
    "    create_metrics_df\n",
    ")\n",
    "\n",
    "from sslsv.evaluations.CosineSVEvaluation import CosineSVEvaluation, CosineSVEvaluationTaskConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7c468fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "models = load_models([\n",
    "    './models/old/vox2_ddp_sntxent_s=30_m=0/config.yml',\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39541851",
   "metadata": {},
   "outputs": [],
   "source": [
    "class CosineSVWithPCAEvaluation(CosineSVEvaluation):\n",
    "    \n",
    "    def _prepare_evaluation(self):\n",
    "        super()._prepare_evaluation()\n",
    "        \n",
    "        # Train PCA\n",
    "        Z = np.array([t.mean(dim=0).numpy() for t in self.test_embeddings.values()])\n",
    "        self.pca = PCA(n_components=200)\n",
    "        self.pca.fit(Z)\n",
    "    \n",
    "    def _get_sv_score(self, a, b):\n",
    "        enrol = self.test_embeddings[a]\n",
    "        test = self.test_embeddings[b]\n",
    "        \n",
    "        enrol = torch.from_numpy(self.pca.transform(enrol.numpy()))\n",
    "        test = torch.from_numpy(self.pca.transform(test.numpy()))\n",
    "\n",
    "        score = self._compute_score(enrol, test)\n",
    "\n",
    "        return score.item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "309d7899",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluate_models(models, CosineSVEvaluation, CosineSVEvaluationTaskConfig())\n",
    "create_metrics_df(models)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43a0df82",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluate_models(models, CosineSVWithPCAEvaluation, CosineSVEvaluationTaskConfig())\n",
    "create_metrics_df(models)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
